///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_APP_CCMDLINE_H_
#define _BUOLA_APP_CCMDLINE_H_

#include <buola/buola.h>
#include <buola/app/ccmdlineoption.h>
#include <map>

namespace buola {

///\addtogroup application
///@{

///////////////////////////////////////////////////////////////////////////

/// command line

/// Objects of this class represent possible command-line options for the
/// program.
///
/// When the object is created, it gets registered as a possible program
/// option. Then, after parsing, the same object can be queried to know if
/// the option was provided.
///
////////////////////////////////////////////////////////////////////////////

class CCmdLine
{
public:
//! \name initialization
//@{
    /// default constructor
    CCmdLine()
        :   mParsed(false)
    {}

    CCmdLine(int pNArg,char **pArgs)
        :   mParsed(false)
    {
        Parse(pNArg,pArgs);
    }
    
    void Parse(int pNArg,char **pArgs);
//@}

/// \name Static interface
//@{
    /// prints a usage message with all the available command-line options
    static void DisplayHelp();
//@}


//! \name parsed options
//! it only makes sense to call these functions after command line parsing
//! has been performed during the initialization of CApp
//@{
    /// check if the option was specified
    bool IsSet(const CCmdLineFlag &pFlag) const
    {
        return mOptions.count(&pFlag);
    }

    const std::string &GetCommand() {   return mCommand;    }

    /// 
    template<typename tType>
    const tType &GetValue(const CCmdLineOption<tType> &pOption) const
    {
        auto i=mOptions.find(&pOption);
        if(i!=mOptions.end())
            return *(const tType*)i->second;
        return pOption.GetDefault();
    }
    
    //! get the list of parsed args
    const std::vector<std::string> &Args() const
    {
        return mArgs;
    }

    //! get the list of parsed args
    int ArgCount() const
    {
        return mArgs.size();
    }

    const std::string &Arg(int i) const
    {
        return mArgs[i];
    }

//@}

private:
    std::map<const CCmdLineFlag*,void*> mOptions;
    std::string mCommand;
    std::vector<std::string> mArgs;
    bool mParsed;
};

CCmdLine &cmd_line();

inline CCmdLineFlag::operator bool() const
{
    return cmd_line().IsSet(*this);
}

template<typename tType>
inline const tType* CCmdLineOption<tType>::operator->() const
{
    return &cmd_line().GetValue(*this);
}

template<typename tType>
inline const tType& CCmdLineOption<tType>::operator*() const
{
    return cmd_line().GetValue(*this);
}

///@}

/*namespace buola*/ }

#endif
